package top.buyibuyi.course.controller;

import com.alibaba.excel.EasyExcel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.buyibuyi.course.R;
import top.buyibuyi.course.esayExcel.ExcelStuSchose;
import top.buyibuyi.course.esayExcel.ExcelfFower;
import top.buyibuyi.course.pojo.StuSchose;
import top.buyibuyi.course.service.SelectStudentByChoosecourseService;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@RestController
@CrossOrigin
@RequestMapping("/teacher")
public class SelectStudentByChoosecourseController {

    @Autowired
    private SelectStudentByChoosecourseService selectStudentByChoosecourseService;

    @GetMapping("/selectStuByChooseid/{chooseid}")
    public R selectStuByChooseid(@PathVariable int chooseid){
        List<StuSchose> stuSchoses = selectStudentByChoosecourseService.selectStudentByChooseid(chooseid);

        List<ExcelfFower> list = new ArrayList<ExcelfFower>();

        for (int i = 0; i < stuSchoses.size(); i++) {
            ExcelfFower data = new ExcelfFower();
            data.setStuid(stuSchoses.get(i).getStuid());
            data.setSname(stuSchoses.get(i).getSname());
            data.setCollege(stuSchoses.get(i).getCollege());
            list.add(data);
        }
        String uuid = UUID.randomUUID().toString().replaceAll("-","");
        String fileName = "D:\\tomcat\\"+uuid+".xlsx";

        EasyExcel.write(fileName, ExcelfFower.class).sheet("学生成绩").doWrite(list);
        return R.ok().data("data",stuSchoses);
    }

    @GetMapping("/selectStuTemplateByChooseid/{chooseid}")
    public R selectStuTemplateByChooseid(@PathVariable int chooseid){
        List<StuSchose> stuSchoses = selectStudentByChoosecourseService.selectStudentByChooseid(chooseid);

        List<ExcelStuSchose> list = new ArrayList<ExcelStuSchose>();

        for (int i = 0; i < stuSchoses.size(); i++) {
            ExcelStuSchose data = new ExcelStuSchose();
            data.setStuid(stuSchoses.get(i).getStuid());
            data.setSname(stuSchoses.get(i).getSname());
            data.setCollege(stuSchoses.get(i).getCollege());
            data.setChooseid(chooseid);
            list.add(data);
        }
        String uuid = UUID.randomUUID().toString().replaceAll("-","");
        String fileName = "D:\\tomcat\\"+uuid+".xlsx";

        EasyExcel.write(fileName, ExcelStuSchose.class).sheet("学生成绩").doWrite(list);
        return R.ok().data("data",stuSchoses);
    }
}
